package XZK010301AND002;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

/**
 * 业务目标:让孙工找到当月休息日
 * 技能目标:综合运用循环判断分支结构,对程序进行格式化输出
 *
 * @author bing.z 2021/6/3 16:19
 **/
public class Demo {
    static Scanner input = new Scanner(System.in);
    static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) throws ParseException {
        while (true) {
            getFreeDay();
        }
    }

    /**
     * 某公司软件开发工程师孙工,作息规律为上三天班,休息一天,经常不确定休息日
     * 是否周末,为此,请你开发一个程序,当孙工输入年及月,以日历方式显示对应月
     * 份的休息日,用中括号进行标记.同时,统计出本月有几天休息,轮到周末休息有
     * 几天.(注:首次休息日是 2019 年 2 月 2 日)
     */
    public static void getFreeDay() throws ParseException {
        Date baseDate = format.parse("2019-2-2");
        String yearAndMonth = "";
        try {
            // 1.获取输入的年月
            yearAndMonth = getYearAndMonth();
        } catch (RuntimeException e) {
            System.out.println(e.getMessage());
            return;
        }
        String temp = yearAndMonth + "-2";
        Date date = format.parse(temp);
        int freeDay = 1;
        // 如果在 2019-2 之前的话，说明没有休息日
        if (date.before(baseDate)) {
            System.out.println("2019年2月之前无休息日...");
            return;
        }
        date = format.parse(yearAndMonth + "-1");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 2.确定本月第一天休息的日子
        int days = calendar.getActualMaximum(Calendar.DATE);
        Date tempDate;
        for (int d = 1; d <= days; d++) {
            tempDate = format.parse(yearAndMonth + "-" + d);
            if (judgeFreeDay(baseDate, tempDate)) {
                calendar.setTime(tempDate);
                freeDay = calendar.get(Calendar.DAY_OF_WEEK);
                break;
            }
        }
        // 3.打印日历并统计休息日天数
        int all = 0, weekend = 0;
        boolean newWeek = true;
        System.out.printf("%-5s\t", "星期日");
        System.out.printf("%-5s\t", "星期一");
        System.out.printf("%-5s\t", "星期二");
        System.out.printf("%-5s\t", "星期三");
        System.out.printf("%-5s\t", "星期四");
        System.out.printf("%-5s\t", "星期五");
        System.out.printf("%-5s\t\n", "星期六");
        for (int d = 1; d <= days; d++) {
            tempDate = format.parse(yearAndMonth + "-" + d);
            calendar.setTime(tempDate);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            if (newWeek) {
                newWeek = false;
                for (int i = 1; i < dayOfWeek; i++) {
                    System.out.print("\t\t");
                }
            } else {
                System.out.print("\t");
            }
            if (dayOfWeek == freeDay) {
                all++;
                if (dayOfWeek == 1 || dayOfWeek == 7) {
                    weekend++;
                }
                System.out.printf("%-5s", "[" + d + "]");
                freeDay = (freeDay + 4) % 7;
                if (freeDay == 0) {
                    freeDay = 7;
                }
            } else {
                System.out.printf("%-5s", d);
            }
            if (dayOfWeek == 7) {
                newWeek = true;
                System.out.println();
            }
        }
        System.out.println();
        System.out.println("本月休息天数有：" + all + "天");
        System.out.println("本月轮到周末休息天数有：" + weekend + "天");
    }

    /**
     * 如果 date 与 baseDate 天数差能整除4的话，说明是休息日
     *
     * @param baseDate 第一个休息日日期
     * @param date     当前日期
     * @return 当前日期是否为休息日
     */
    private static boolean judgeFreeDay(Date baseDate, Date date) {
        int diff = (int) ((date.getTime() - baseDate.getTime()) / (1000 * 3600 * 24));
        return diff % 4 == 0;
    }

    /**
     * 获取输入年月字符串
     *
     * @return 年月字符串
     */
    private static String getYearAndMonth() throws RuntimeException {
        System.out.println("请输入年:");
        String year = input.nextLine();
        year = year.trim();
        int yearI = Integer.parseInt(year);
        System.out.println("请输入月:");
        String month = input.nextLine();
        month = month.trim();
        int monthI = Integer.parseInt(month);
        if (yearI <= 0 || yearI > 9999 || monthI <= 0 || monthI > 12) {
            throw new RuntimeException("输入的年月不正确，请重新输入");
        }
        return year + "-" + month;
    }
}
